package com.blue.mountain.bluemountainjdk17.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    /**
     * 配置跨域请求的相关规则
     * @param registry 跨域注册器，用于添加跨域映射规则
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 配置所有的请求路径都允许跨域访问
        registry.addMapping("/**")
                // 允许所有的源进行跨域访问
                .allowedOriginPatterns("*") // 允许的来源，可以是具体的 URL 或 "*"
                // 允许的请求方法
                .allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS")
                // 允许所有的请求头
                .allowedHeaders("*")
                // 允许携带凭证（如 Cookie）
                .allowCredentials(true)
                // 预检请求的缓存时间，单位为秒
                .maxAge(3600);
    }

    // @Override
    // public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //     registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    //     registry.addResourceHandler("doc.html")
    //             .addResourceLocations("classpath:/META-INF/resources/");
    //     registry.addResourceHandler("/swagger-ui/**")
    //             .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
    //     registry.addResourceHandler("/webjars/**")
    //             .addResourceLocations("classpath:/META-INF/resources/webjars/");
    // }
}
